package com.example.huanhang.changzhouapp.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.activity.AboutActivity;
import com.example.huanhang.changzhouapp.activity.LoginActivity;
import com.example.huanhang.changzhouapp.activity.UpdatePwdActivity;
import com.example.huanhang.changzhouapp.base.BaseLazyFragment;
import com.example.huanhang.changzhouapp.bean.User;
import com.example.huanhang.changzhouapp.util.ToastUtil;
import com.gyf.barlibrary.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.MODE_PRIVATE;
import static com.example.huanhang.changzhouapp.util.ConstantUtils.IS_WRITE_OFF;

/**
 * @author : lei
 * @desc :
 * @date : 2018/9/4 0004  下午 2:29.
 * 个人博客站: http://www.bestlei.top
 */

public class MineFragment extends BaseLazyFragment implements View.OnClickListener {

    @BindView(R.id.tv_title)
    AppCompatTextView mTvTitle;
    @BindView(R.id.title)
    RelativeLayout mTitle;
    @BindView(R.id.main_head_img)
    CircleImageView mMainHeadImg;
    @BindView(R.id.main_name)
    TextView mMainName;
    @BindView(R.id.st_login_name)
    SuperTextView mStLoginName;
//    @BindView(R.id.st_sex)
//    SuperTextView mStSex;
    @BindView(R.id.st_state)
    SuperTextView mStState;
    @BindView(R.id.st_zhgl)
    SuperTextView mStZhgl;
    @BindView(R.id.st_xiugai)
    SuperTextView mxiugai;
    @BindView(R.id.st_about)
    SuperTextView mAbout;
    SharedPreferences mSharedPreferences;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        ImmersionBar.setTitleBar(getActivity(), mTitle);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("我的");
        mStZhgl.setOnClickListener(this);
        mxiugai.setOnClickListener(this);
        mAbout.setOnClickListener(this);

    }
    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColorTransformEnable(false)
                .init();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(User user) {
        mMainName.setText(user.getGzName());
        mStLoginName.setRightString(user.getName());
//        mStSex.setRightString(user.getXb());
        mStState.setRightString(user.getState());

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.st_zhgl:
                new AlertDialog.Builder(getActivity())
                        .setMessage("确定退出账号?")
                        .setPositiveButton("确认", (dialog, which) -> {
                            Intent intent1 = new Intent(getActivity(), LoginActivity.class);
                            intent1.putExtra(IS_WRITE_OFF, IS_WRITE_OFF);
                            intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent1);
                        })
                        .setNegativeButton("取消",null)
                        .show();
                break;
            case R.id.st_xiugai:
                updatePassWord();
                break;
            case R.id.st_about:
              //关于
                Intent intent=new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
                break;

        }
    }

    private void updatePassWord() {
        mSharedPreferences =getActivity().getSharedPreferences("login", MODE_PRIVATE);
        String passWord = mSharedPreferences.getString("passWord", "");
        View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_update_pwd, null);
            AppCompatEditText oldPwd = dialogView.findViewById(R.id.dialog_pwd);
            new AlertDialog.Builder(getActivity()).setView(dialogView)
                    .setPositiveButton("确定", (dialogInterface, i) -> {
                        String oldTrim = oldPwd.getText().toString().trim();
                        if (TextUtils.isEmpty(oldTrim)) {
                            ToastUtil.toast(getActivity(),"请填写密码");
                        } else {
                            if (oldTrim.equals(passWord)) {
                                Intent intent=new Intent(getActivity(),UpdatePwdActivity.class);
                                startActivity(intent);
                            } else {
                                ToastUtil.toast(getActivity(),"原密码错误");
                            }
                        }
                    }).setNegativeButton("取消", (dialogInterface, i) -> {

            }).show();

    }
}
